/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年5月7日 下午4:28:12
 */
package com.absir.appserv.master.api;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import com.absir.appserv.system.bean.value.JaLang;
import com.absir.appserv.system.crud.UploadCrudFactory;
import com.absir.appserv.upgrade.UpgradeService;
import com.absir.bean.basis.Base;
import com.absir.bean.core.BeanFactoryUtils;
import com.absir.core.helper.HelperFile;
import com.absir.core.helper.HelperFileName;
import com.absir.core.helper.HelperIO;
import com.absir.server.in.Input;
import com.absir.server.value.Server;

/**
 * @author absir
 *
 */
@Base
@Server
public class api_master extends ApiMaster {

	@JaLang("时间")
	public long time() {
		return System.currentTimeMillis();
	}

	@JaLang("获取升级文件")
	public void upgrade(String filepath, HttpServletResponse response) throws IOException {
		File file = new File(UpgradeService.ME.getUpgradeResource() + filepath);
		if (file.exists() && file.isFile()) {
			response.addHeader("Content-Disposition", "attachment;filename=" + HelperFileName.getName(filepath));
			HelperIO.copy(HelperFile.openInputStream(file), response.getOutputStream());
		}
	}

	@JaLang("获取资源文件")
	public void resource(String filepath, HttpServletResponse response) throws IOException {
		File file = new File(BeanFactoryUtils.getBeanConfig().getResourcePath() + filepath);
		if (file.exists() && file.isFile()) {
			response.addHeader("Content-Disposition", "attachment;filename=" + HelperFileName.getName(filepath));
			HelperIO.copy(HelperFile.openInputStream(file), response.getOutputStream());
		}
	}

	@JaLang("上传文件")
	public void upload(String uploadFile, Input input) throws IOException {
		UploadCrudFactory.ME.upload(uploadFile, input.getInputStream());
	}

	@JaLang("获取上传文件")
	public void download(String filepath, HttpServletResponse response) throws IOException {
		File file = new File(UploadCrudFactory.getUploadPath() + filepath);
		if (file.exists() && file.isFile()) {
			response.addHeader("Content-Disposition", "attachment;filename=" + HelperFileName.getName(filepath));
			HelperIO.copy(HelperFile.openInputStream(file), response.getOutputStream());
		}
	}
}
